﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using Ue.Qcdz.Domain.Articles;
using Ue.Qcdz.Domain.TreeViews;
namespace Ue.Qcdz.Repositories.Articles
{
    public class ArticleDataQueryWhereClause : Ue.Infrastructure.RepositoryFramework.Repository.IWhereClause
    {
        private IArticleDataQuery articleDataQuery;

        public ArticleDataQueryWhereClause(IArticleDataQuery articleDataQuery)
        {
            this.articleDataQuery = articleDataQuery;
        }

        #region IWhereClause 成员

        public string Clause
        {
            get
            {
                //单页 类别的新闻不显示
                string whereClause = "1=1";
                //指定按标题搜索
                if (!string.IsNullOrEmpty(this.articleDataQuery.Title))
                    whereClause += string.Format(" and {0} like '%{1}%'", ArticleFactory.FieldNames.Title, articleDataQuery.Title);
                //指定 是否推荐
                if (articleDataQuery.IsTop)
                    whereClause += " and IsTop =1";
                //指定是否可用（即状态时正常的，已经发布的信息）
                if (articleDataQuery.IsUse)
                    whereClause += " and Status=1";
                else
                    whereClause += " and Status=0";
                //是否包含标题图片
                if (articleDataQuery.IsImg)
                    whereClause += " and Len(ImageUrl)>0";
                //是否是焦点新闻
                if (articleDataQuery.IsFocus)
                    whereClause += " and IsFocus=1";
                //指定按关键字搜索
                if (!string.IsNullOrEmpty(this.articleDataQuery.Keyword))
                {
                    string[] keyArr = this.articleDataQuery.Keyword.Split(new string[] { ",", "，", ";", "；" }, System.StringSplitOptions.RemoveEmptyEntries);

                    whereClause += " and (";
                    foreach (string item in keyArr)
                    {
                        whereClause += string.Format("{0} like '%{1}%' or ", ArticleFactory.FieldNames.Title, item);
                        whereClause += string.Format("{0} like '%{1}%' or ", ArticleFactory.FieldNames.Keyword, item);
                    }
                    whereClause = whereClause.Substring(0, whereClause.Length - 4);
                    whereClause += ")";
                }
                //指定 TagId
                if (!string.IsNullOrEmpty(this.articleDataQuery.NodeId))
                {
                    //读取该节点的所有栏目
                    TreeNode node = ArticleService.TagTreeView.FindNodeBy(new Guid(this.articleDataQuery.NodeId));

                    //循环生成 SQL 语句
                    if (node != null)
                    {
                        whereClause += " and (";
                        whereClause += string.Format("{0}='{1}' or ", ArticleFactory.FieldNames.TagId, node.Tag.Key);


                        var childNodes = node.ChildNodes;//需要将nodes影射到新的IEnumable当中，否则如果在foreach过程中ChildNodes对象发生改变。就会报集合已修改；可能无法执行枚举操作的错误

                        foreach (TreeNode item in childNodes)
                        {
                            whereClause += string.Format("{0}='{1}' or ", ArticleFactory.FieldNames.TagId, item.Tag.Key);
                        }
                        
                        //同时指定了 额外的 TagId
                        if (!string.IsNullOrEmpty(this.articleDataQuery.TagId))
                        {
                            whereClause += string.Format("{0}=@{0} or ", ArticleFactory.FieldNames.TagId);
                        }
                        //
                        whereClause = whereClause.Substring(0, whereClause.Length - 4);
                        whereClause += ")";
                    }
                }
                else
                {
                    //在没有指定节点，而指定了具体的 TagId
                    if (!string.IsNullOrEmpty(this.articleDataQuery.TagId))
                        whereClause += string.Format(" and {0} = @{0}", ArticleFactory.FieldNames.TagId);
                }

                return whereClause;
            }
        }

        public IDataParameter[] Parameters
        {
            get
            {
                return new IDataParameter[] {
                    SqlServerDriver.Instance.BuildParameter(ArticleFactory.FieldNames.Title, this.articleDataQuery.Title),
                    SqlServerDriver.Instance.BuildParameter(ArticleFactory.FieldNames.TagId,this.articleDataQuery.TagId)                
                };

            }
        }

        #endregion
    }
}
